<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title"><img src="../../assets/login-1.png" alt=""></h3>
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="请输入账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <div class="wrap-yzm">
        <el-form-item prop="yanzhengma" class="yzm" inline>
          <span class="svg-container">
            <svg-icon icon-class="yanzhengma" />
          </span>
          <el-input
            ref="code"
            v-model="loginForm.code"
            placeholder="请输入验证码"
            name="yanzhengma"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <div v-if="isshow" @click="getYzm">
          <img
            src="https://likede2-java.itheima.net/api/user-service/user/imageCode/MLQoJopuBQxKCGMxowgIVeWQlmbsrZyU"
            alt=""
          >
        </div>
        <div v-else @click="getYzm">
          <img
            src="https://likede2-java.itheima.net/api/user-service/user/imageCode/40iu2UWaGzMaFEjHPPZEJ5HJ4yIDHxOC"
            alt=""
          >
        </div>
      </div>

      <el-button :loading="loading" type="primary" class="login-btn" @click="submitLogin">登录</el-button>

    </el-form>
  </div>
</template>

<script>
import { yzmAPI } from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: 'MLQoJopuBQxKCGMxowgIVeWQlmbsrZyU',
        loginType: 0
      },
      loginRules: {
        loginName: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      isshow: true
    }
  },
  methods: {
    showPwd() {
      this.passwordType === 'password' ? this.passwordType = '' : this.passwordType = 'password'
      this.$nextTick(() => { this.$refs.password.focus() })
    },
    async submitLogin() {
      this.$refs.loginForm.validate(async vali => {
        if (vali) {
          // console.log(vali)
          // 接口待用
          try {
            this.loading = true
            await this.$store.dispatch('user/loginAction', this.loginForm)
          } finally {
            this.loading = false
          }
          // 完善登录
          this.$router.push('/')
        }
      })
    },
    async getYzm() {
      this.isshow = !this.isshow
      await yzmAPI(this.$store.state.user.clientToken)
      console.log(yzmAPI(this.$store.state.user.clientToken))
    }

  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
      color: #999 !important;;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background-color: #fff;
    border-radius: 5px;
    color: #454545;
    border: 1px solid #e2e2e2;
    border-radius: 4px;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url('~@/assets/login-bgc.jpg');
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 10px 35px ;
    margin: 280px auto 0;
    background-color: #fff;
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      position: relative;
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
      > img {
      position: absolute;
      left: 40%;
      bottom: 20%;
      width: 96px;
      height: 96px;
      }
    }
  }
  .wrap-yzm{
    display: flex;
    .yzm{
      flex: 1;
    }
  }
.login-btn{
   width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
}

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
